package net.zjitc.seqqueue.myservice;

import net.zjitc.exception.MyQuException;
import net.zjitc.seqqueue.IQueue;
import net.zjitc.seqqueue.SeqQueue;

import java.util.Scanner;

/**
 * Created by Administrator on 2017/4/6.
 */
public class MychineQueue extends Thread{
    /*队列由机器产生*/
        private IQueue<Integer> queue;
        private Integer number=0;
    /*接收最大容量maxlength
    * 然后根据maxlength来new一个队列出来，交赋值给栈
    * */
    public MychineQueue(Integer maxlength) {
        queue=new SeqQueue<>(maxlength);
    }

    public void run() {
        /*键盘输入，当按回车时产生号码*/
        Scanner sc=new Scanner(System.in);
        while (true){
            System.out.println("请回车取号码");
            sc.nextLine();
            /*已经按下回车，怎么处理*/
            number++;
            Integer preNum=number;//号码产生后要入队的
            try {/*捕获异常信息（当队列满时的捕获）*/
                queue.enqueue(preNum);
                System.out.println("你是"+preNum+"号，你前面有"+(queue.size()-1)+"个人在排队");
            } catch (MyQuException e) {
               number--;
                System.out.println(e.getMessage());
            }
        }
    }
    public IQueue<Integer> getQueue(){
        return this.queue;
    }
}
